from util import *
from const import const

# myAttack = 10000 # 我的实力

# 演习四个框框的位置
EXERCISE_SIZES = ((157, 170, 202, 217),
                  (322, 173, 364, 212),
                  (487, 175, 530, 212),
                  (654, 173, 697, 212))

def FindOKExercise():
    """ 演习中的四个里面找到最小值 
    
    :returns: (min, minIndex)  最小值 最小值所在的序号
    """
    min = 999999
    minIndex = -1
    index = -1
    for sizeValue in EXERCISE_SIZES:
        index += 1
        list = []
        while len(list) == 0:
            list = util.getNumbers(sizeValue)
        sum = 0
        avg = 999999
        for value in list:
            sum += int(value)
        avg = sum // 2
        if(avg < min):
            min = avg
            minIndex = index
    return (min, minIndex)

def main():
    util.logOut(__file__,'演习脚本开始')
    # 判断是否在演习界面
    if not util.isFindPic(const.publicPath() + 'bmp/is_exercise.jpg'):
        # 返回主页面
        const.backMainProcess()
        # 进入演习界面
        x, y = const.picLoop(const.publicPath() + 'bmp/weigh anchor.jpg')
        util.click(x, y)
        x, y, url = const.picMulLoop([
            const.publicPath() + 'bmp/exercise.jpg', # 演习
            const.publicPath() + 'bmp/withdraw.jpg', # 在图里
        ])
        if url == const.publicPath() + 'bmp/exercise.jpg':
            pass
        elif url == const.publicPath() + 'bmp/withdraw.jpg':
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/OK.jpg')
            util.click(x, y)
            const.backMainProcess()
            x, y = const.picLoop(const.publicPath() + 'bmp/weigh anchor.jpg')
            util.click(x, y)
            x, y = const.picLoop(const.publicPath() + 'bmp/exercise.jpg')
        util.click(x, y)
        const.picLoop(const.publicPath() + 'bmp/is_exercise.jpg')
    while True:
        # 0/10退出
        time.sleep(1)
        list = util.getNumbers((778, 91, 818, 113))
        if list != [] and list[0][0] == '0':
            const.backMainProcess()
            return

        # 识字 选择弱的
        min, minIndex = FindOKExercise()

        util.click(EXERCISE_SIZES[minIndex][0], EXERCISE_SIZES[minIndex][1])
        time.sleep(.500)

        # 开始演习
        x, y = const.picLoop(const.publicPath() + 'bmp/start excise.jpg')
        util.click(x, y)
        time.sleep(.500)
        const.anchorProcess()
